package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class AuthorHorizontalAdapter extends RecyclerView.Adapter<AuthorHorizontalAdapter.ViewHolder>{

    private List<DFHomeNovelBeans> list1,list2;
    private Context context;
    private String type;
    private String pageType;
    private EventBean eventBean;
    public AuthorHorizontalAdapter(Context context, List<DFHomeNovelBeans> list1,List<DFHomeNovelBeans> list2,
                                   String type,EventBean eventBean,String pageType){
        this.list1 = list1;
        this.list2 = list2;
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        this.eventBean = eventBean;
    }

    public DFHomeNovelBeans getItem(int position){
        if (list1 != null && list1.size()>position) {
            return list1.get(position);
        }
        return null;
    }

    public DFHomeNovelBeans getItem2(int position){
        if (list2 != null && list2.size()>position) {
            return list2.get(position);
        }
        return null;
    }

    public void refrush(List<DFHomeNovelBeans> list1,List<DFHomeNovelBeans> list2) {
        if (list1 != null) {
            this.list1 = list1;
            if (list2 != null) {
                this.list2 = list2;
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.author_horizontal_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        holder.tvTitle2 = (TextView) view.findViewById(R.id.tvTitle2);
        holder.ivHead = (ImageView) view.findViewById(R.id.ivHead);
        holder.ivHead2 = (ImageView) view.findViewById(R.id.ivHead2);
        holder.lay = view.findViewById(R.id.lay);
        holder.rlOne = (RelativeLayout) view.findViewById(R.id.rlOne);
        holder.rlTwo = (RelativeLayout) view.findViewById(R.id.rlTwo);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final DFHomeNovelBeans model = getItem(position);
        if (model != null && model.getUser() != null) {
            holder.tvTitle.setText(model.getUser().getNickname());
            if (!TextUtils.isEmpty(model.getUser().getIcon())) {
                GlideUtil.displayCircleImageBorder(context, holder.ivHead, model.getUser().getIcon(),
                        1, Color.parseColor("#e5e5e5"));
            }
            if (position == 0) {
                holder.lay.setPadding(ScreenUtil.dip2px(context,12),0,0,0);
            } else {
                holder.lay.setPadding(0,0,0,0);
            }
            final DFHomeNovelBeans model2 = getItem2(position);
            if (model2 != null && model2.getUser() != null) {
                holder.rlTwo.setVisibility(View.VISIBLE);
                holder.tvTitle2.setText(model2.getUser().getNickname());
                if (!TextUtils.isEmpty(model2.getUser().getIcon())) {
                    GlideUtil.displayCircleImageBorder(context, holder.ivHead2, model2.getUser().getIcon(),
                            1, Color.parseColor("#e5e5e5"));
                }
                holder.rlTwo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                        if (model2.getUser() != null) {
                            UserDetailActivity.Companion.startActivity(context,model2.getUser().getUid());
                        }
                    }
                });
            }else{
                holder.rlTwo.setVisibility(View.GONE);
            }
            holder.rlOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                    if (model.getUser() != null) {
                        UserDetailActivity.Companion.startActivity(context,model.getUser().getUid());
                    }
                }
            });
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list1 != null) {
            return list1.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle;
        TextView tvTitle2;
        ImageView ivHead;
        ImageView ivHead2;
        RelativeLayout rlOne,rlTwo;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.tvTitle2.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.ivHead.setAlpha(1.0f);
        viewHolder.ivHead2.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.tvTitle2.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivHead.setAlpha(Constant.imagViewAlpha);
        viewHolder.ivHead2.setAlpha(Constant.imagViewAlpha);
    }
}
